#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Description: 常量文件
import threading

from utils.parse import get_parse_args
from pathlib import Path

ENV_PORT = 22
ENV_PASSWORD = "Cljslrl0620!"

local_data = threading.local()

project_dir = Path.cwd()
# templates路径
templates_path = project_dir / 'templates'

VDBENCH_SMALL_FILE_TEMPLATE = "vdbench_small_file_template.jinja2"
VDBENCH_SMALL_FILE = "/root/bench/vdbench_small_file"
VDBENCH_VERIFY_FILE_TEMPLATE = "vdbench_verify_file_template.jinja2"
VDBENCH_VERIFY_FILE = "/root/bench/vdbench_verify_file"
FIO_BIG_FILE_TEMPLATE = "fio_big_file_template.jinja2"
FIO_BIG_FILE = "/root/bench/fio_big_file.fio"
SYSBENCH_INSERT = "oltp_insert.lua"
SYSBENCH_INSERT_FILE = "/root/bench/oltp_insert.lua"
SYSBENCH_READ_WRITE = "oltp_read_write.lua"
SYSBENCH_READ_WRITE_FILE = "/root/bench/oltp_read_write.lua"

parse_args = get_parse_args()

if parse_args.mode == "nocow":
    QDFS_CREATE = f"qdfs create -f --bucket=512KiB --replicas={parse_args.replicas} --block_size=4k --nocow --metadata_checksum=none --data_checksum=none "
else:
    QDFS_CREATE = f"qdfs create -f --bucket=512KiB --replicas={parse_args.replicas} --block_size=4k --metadata_checksum=none --data_checksum=none "

QDFS_MOUNT = "mount -t qdfs {0} {1}"
QDFS_DEV_ADD = "qdfs dev add {0} {1} -f"
QDFS_DEV_REMOVE = "qdfs dev remove {0} {1}"
QDFS_DEV_ONLINE = "qdfs dev online {0}"
QDFS_DEV_OFFLINE = "qdfs dev offline -f {0}"
QDFS_DATA_REREPLICATE = "qdfs data rereplicate {0}"

SYSTEM_UMOUNT = "umount {0}"
SYSTEM_DU_SH = "du -sh {0} | awk '{{print $1}}'"
SYSTEM_CHOWN = "chown -R admin:admin {0}"
SYSTEM_CHMOD = "chmod -R 777 {0}"
SYSTEM_RM_RF = "rm -rf {0}/* &"
SYSTEM_NVME_LIST = "nvme list"
SYSTEM_LL_QDISK = "ls -l /dev/qdisk/* | awk '/nvme/' | awk ' !/p[0-9]/'"
SYSTEM_LSPCI_SLOT = "grep {0} /sys/bus/pci/slots/*/address"
SYSTEM_SLOT_PATH = "ls -l /sys/class/nvme"
SYSTEM_DF_TH_CAPACITY = "df -Th | grep '{0}' | awk '{{print $5}}'"
BENCH_FIO = "fio {0} &"
BENCH_VDBENCH_FILE = "/root/vdbench/vdbench -f {0}"
BENCH_FALLOCATE = "fallocate -l {0} {1}/fallocatedata"


QDFS_SHOW_RW_STATUS = "qdfs show -h {0} | grep State | awk '{{print $2}}'"
QDFS_SHOW_DEVICE = "qdfs show -h {0} | grep device"
QDFS_SHOW_ERROR_COUNT = "qdfs show -h {0} | grep -E 'device|errors'"

"""
=============== 以下是check相关命令===============
"""
CHECK_VDBFILE = "ls {0}/ -lrt | grep vdb | awk '{{print $9}}'"
CHECK_FILE_GROUP = "tree -u -g -h {0} | grep -v admin | grep -v {0} | grep -v 'directories'"
CHECK_FILE_AUTH = "tree -p -h {0} | grep -v 'rwxrwxrwx' | grep -v {0} | grep -v directories | awk '{{print $4}}'"
CHECK_SPACE = "ls -l {0}/ | grep -v total"
CHECK_REPLICAS_DISK = "qdfs show {0} -h  | grep 'user:' | grep -o '\\[.*\\]' | sed 's/\\[\\|\\]//g'"
CHECK_QDFS_MOUNT = ""

QDFS_CHECK_BUCKET_SIZE = "qdfs dev get-super {0}  | grep -i 'bucket size' | cut -d ':' -f 2 | tr -d ' '"
QDFS_CHECK_BLOCK_SIZE = "qdfs dev get-super {0}  | grep -i 'block_size' | cut -d ':' -f 2 | tr -d ' '"
QDFS_CHECK_METADATA_CHECKSUM = "qdfs dev get-super {0}  | grep -i metadata_checksum | cut -d ' ' -f27"
QDFS_CHECK_DATA_CHECKSUM = "qdfs dev get-super {0}  | grep -i data_checksum | tail -n 1 | cut -d ' ' -f31"
QDFS_REPLICAS_CHECK = "qdfs dev get-super {0}  | grep -i 'metadata_replicas:' | cut -d ' ' -f27"

